import redis
import app_run_time
import logging
import config_default
import json


try:
	connect_pool=redis.ConnectionPool(**config_default.configs['redis'])
except BaseException as e:
	logging.error(e)


def get(key):
	api=redis.Redis(connection_pool=connect_pool)
	val=api.get(key)
	#val=api.get(json.loads(val_i).get('key')) if val_i else None
	api.close()
	return json.loads(val) if val else None

def delete(key):
	api=redis.Redis(connection_pool=connect_pool)
	result=api.delete(key)
	return result





def close_redis():
	logging.info('close redis pool !!!!')
	try:
		if connect_pool:
			connect_pool.disconnect(True)
	except BaseException as e:
		logging.error(e)

app_run_time.add_app_exit_fun(close_redis)


if __name__ == '__main__':
	app_run_time.app_start()

	logging.debug(get('lastFiveMinNetflow::rEllO7vm').get('avg'))
	#logging.debug(get('scriptAllConfig::all'))
	#logging.debug(delete("scriptAllConfig::all"))
	#logging.debug(delete("scriptAllConfig"))
	#logging.debug(get('scriptAllConfig::all'))
	app_run_time.app_exit_befor()
